package io.plus.user_interface.common.protocol;

import io.plus.interactive.AsynchronousSocketChannelContext;
import io.plus.user_interface.common.SessionContext;
import io.plus.user_interface.common.packet.Command;


public class PacketConverter implements IPacketConverter {

    @Override
    public PacketImpl converteRequestPacket(byte[] body, Command command, AsynchronousSocketChannelContext asynchronousSocketChannelContext) {
        Object sessionContext = asynchronousSocketChannelContext.getAttribute();
        if (sessionContext instanceof SessionContext) {//转TCP协议请求包;
            PacketImpl packet = new PacketImpl(command, body);
            Encoder.encode(packet, asynchronousSocketChannelContext.getClientConfiguration(), asynchronousSocketChannelContext);
            packet.setCommand(command);
            return packet;
        }
        return null;
    }

    @Override
    public PacketImpl converteResponsepPacket(byte[] body, Command command, AsynchronousSocketChannelContext asynchronousSocketChannelContext) {
        Object sessionContext = asynchronousSocketChannelContext.getAttribute();
        if (sessionContext instanceof SessionContext) {//转TCP协议响应包;
            PacketImpl packet = new PacketImpl(command, body);
            Encoder.encode(packet, asynchronousSocketChannelContext.getClientConfiguration(), asynchronousSocketChannelContext);
            packet.setCommand(command);
            return packet;
        }
        return null;
    }

}
